import VeeValidate, { Validator } from 'vee-validate'
import * as valids from '@/valids'
// 进行验证信息的自定义
const dict = {
  custom: {
    name: {
      required: '名称不能为空',
      max: '名称长度过长'
    },
    account: {
      required: '用户名不能为空',
      max: '用户名长度过长'
    },
    password: {
      required: '密码不能为空',
      min: '密码长度不少于6位',
      max: '密码长度不超过64位'
    },
    repassword: {
      required: '密码确认不能为空',
      confirmed: '两次密码不一致'
    },
    accounts: {
      required: '账号不能为空'
    },
    nickName: {
      required: '昵称不能为空'
    },
    phone: {
      required: '手机号不能为空'
    },
    verification: {
      required: '验证码不能为空'
    },
    email: {
      required: '电子邮箱不能为空'
    },
    contacter: {
      required: '联系人姓名不能为空'
    },
    deviceTotal: {
      required: '授权设备数不能为空'
    },
    apps: {
      required: '至少选择一个应用'
    },
    days: {
      required: '有效期不能为空'
    },
 
    ...valids.orgVaild,
    ...valids.resourcesVaild,
    ...valids.noticeVaild,
    organizations: {
      required: '至少选择一个客户'
    },

    contact: {
      required: '联系人不能为空'
    },
    job: {
      required: '职务不能为空'
    },
    username: {
      required: '用户名不能为空'
    },
    roleName: {
      required: '角色名称不能为空'
    },
    role: {
      required: '角色不能为空'
    }, 
    programName: {
      required: '程序名称不能为空'
    },
    programUrl: {
      required: '请上传更新包'
    },
    version: {
      required: '版本号不能为空'
    },
    model_id: {
      required: '适配机型不能为空'
    }, 
  }
}

// 长度为4位数的 maxLength
Validator.extend('maxLength4', {
  getMessage: field => '最大字符长度为4',
  validate: value => value.length <= 4
})
// 长度为16位数的 maxLength
Validator.extend('maxLength16', {
  getMessage: field => '最大字符长度为16',
  validate: value => value.length <= 16
})
// 长度为30位数的 maxLength
Validator.extend('maxLength30', {
  getMessage: field => '最大字符长度为30',
  validate: value => value.length <= 30
})
// 长度为50位数的 maxLength
Validator.extend('maxLength50', {
  getMessage: field => '最大字符长度为50',
  validate: value => value.length <= 50
})
// 长度为100位数的 maxLength
Validator.extend('maxLength100', {
  getMessage: field => '最大字符长度为100',
  validate: value => value.length <= 100
})
// 长度为120位数的 maxLength
Validator.extend('maxLength120', {
  getMessage: field => '最大字符长度为120',
  validate: value => value.length <= 120
})
// 长度为128位数的 maxLength
Validator.extend('maxLength128', {
  getMessage: field => '最大字符长度为128',
  validate: value => value.length <= 128
})
// 长度为300位数的 maxLength
Validator.extend('maxLength300', {
  getMessage: field => '最大字符长度为300',
  validate: value => value.length <= 300
})
// 只允许数字
Validator.extend('onlyRule0', {
  getMessage: field => '只允许数字',
  validate: value => /^[0-9]+$/.test(value)
})
// 只允许数字、小数点和-
Validator.extend('onlyRule1', {
  getMessage: field => '只允许数字、小数点和"-"字符',
  validate: value => /^[0-9\.-]+$/.test(value)
})
// 只允许数字、小数点
Validator.extend('onlyRule2', {
  getMessage: field => '只允许数字、小数点和"-"字符',
  validate: value => /^[0-9\.]+$/.test(value)
})
// 只允许数字、英文字母、小数点和"-_"字符'
Validator.extend('onlyRule3', {
  getMessage: field => '只允许数字、英文字母".-:_"字符',
  validate: value => /^[a-zA-Z0-9\.:_-]+$/.test(value)
})
// 只允许数字、英文字母、小数点和"-_"字符'
Validator.extend('onlyRule4', {
  getMessage: field => '只允许数字、英文字母".-%_"字符',
  validate: value => /^[a-zA-Z0-9\.:_%-]+$/.test(value)
})
// 只允许正整数
Validator.extend('onlyRule5', {
  getMessage: field => '必须为正整数',
  validate: value => /^[1-9]\d*$/.test(value)
})
// 值小于60
Validator.extend('onlyRule6', {
  getMessage: field => '不能大于60',
  validate: value => parseFloat(value) <= 60
})
// 只允许数字、 英文字母、小 数点 和"-_"字 符'
Validator.extend('onlyRule7', {
  getMessage: field => '只允许中文数字、英文字母"-"字符',
  validate: value => /^[a-zA-Z0-9\u4e00-\u9fa5\-]+$/.test(value)
})
// 只允许数字 0 - 120
Validator.extend('onlyRule8', {
  getMessage: field => '只允许0至120之间数字',
  validate: value => /^(?:[0-9][0-9]?|1[01][0-9]|120)$/.test(value)
})

Validator.extend('phone', {
  getMessage: field => '请输入正确的手机号',
  validate: value => value.length === 11 && /[1][0-9]{10}/.test(value)
})

Validator.extend('email', {
  getMessage: field => '请输入正确的邮箱',
  validate: value => /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value)
})

Validator.extend('phoneAndEmail', {
  getMessage: field => '请输入正确的手机号或邮箱',
  validate: value => value.length === 11 && /[1][0-9]{10}/.test(value) ||  /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value)
})

Validator.extend('specialCharacter', {
  getMessage: field => '存在特殊字符(只能是中文、英文或数字)',
  validate: value => /^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value)
})

Validator.extend('noChinese', {
  getMessage: field => '只能是数字和英文',
  validate: value => /^[A-Za-z0-9]+$/.test(value)
})

Validator.extend('nameLength', {
  getMessage: field => '长度过长',
  validate: value => value.length <= 100
})

Validator.extend('fireNameLength', {
  getMessage: field => '长度过长',
  validate: value => value.length <= 10
})

Validator.extend('remarkLength', {
  getMessage: field => '长度过长',
  validate: value => value.length <= 100
})

Validator.extend('onnumber', {
  getMessage: field => '只允许数字',
  validate: value => /^[0-9]+$/.test(value)
})

export const Config = {
  fieldsBagName: 'veefields'
}
Validator.localize('en', dict)
export default VeeValidate
